package com.suoluo.zenadmin.service.crawler.resolver;

import java.util.List;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;

import com.suoluo.zenadmin.common.config.SystemConfigHolder;
import com.suoluo.zenadmin.service.crawler.bean.HotelReviewBean;
import com.suoluo.zenadmin.service.crawler.bean.HotelReviewRecordBean;



public abstract class AbstractCrawlerResolver implements CrawlerResolver {
	private static final Log logger = LogFactory.getLog("crawler");

	private final CrawlerResovlerTask task;

	public AbstractCrawlerResolver(CrawlerResovlerTask task) {
		this.task = task;
	}

	@Override
	public HotelReviewBean doResolver() {
		HotelReviewBean review = new HotelReviewBean();
		review.setHotelId(task.getHotelId());
		review.setReviewId(task.getReviewId());
		review.setReviewUrl(task.getReviewUrl());
		review.setCrawlerStatus(HotelReviewBean.CRAWLER_SUCCESS);
		// review.setReviewCount(re)
		try {
			Document doc = Jsoup.connect(task.getReviewUrl())
					.timeout(SystemConfigHolder.getCrawlerTimeout()).get();
			// 1.评论总数
			logDebug("resolve review count start");
			int reviewCount = resolveReviewCount(doc);
			review.setReviewCount(reviewCount);
			logDebug("resolve review count success,reviewCount=" + reviewCount);

			// 2.评论的分数
			logDebug("resolve review score start");
			double reviewScore = resolveReviewScore(doc);
			review.setReviewScore(reviewScore);
			logDebug("resolve review score success,reviewScore=" + reviewCount);

			// 3.评分的满分
			logDebug("resolve review full score start");
			double reviewFullScore = resolveReviewFullScore(doc);
			review.setReviewFullScore(reviewFullScore);
			logDebug("resolve review full score success,reviewFullScore="
					+ reviewFullScore);

			// 4.评分的级别
			logDebug("resolve review level start");
			String reviewLevel = resolveReviewLevel(doc);
			review.setReviewLevel(reviewLevel);
			logDebug("resolve review level success,reviewLevel=" + reviewLevel);

			// 5.酒店排名，暂时只有道道有这个排名信息
			logDebug("resolve review rank start");
			int reviewRank = resolveReviewRank(doc);
			review.setReviewRank(reviewRank);
			logDebug("resolve review rank,reviewRank=" + reviewRank);

			// 6.解析评论的记录数
			logDebug("resolve review records start");
			List<HotelReviewRecordBean> records = resolveReviewRecords(doc);
			review.setReviewRecords(records);
			logDebug("resolve review records success,records size="
					+ records.size());

		} catch (Exception e){
			// 抓取失败
			logger.error(
					"hotelId=" + task.getHotelId() + "|reviewId="
							+ task.getReviewId() + "|reviewType="
							+ task.getReviewType() + "|url="+task.getReviewUrl()+"|crawler fail", e);
			// 抓取失败
			review.setCrawlerStatus(HotelReviewBean.CRAWLER_FAIL);
		}
		return review;
	}

	/**
	 * t_review_count int nullable 评论总数 每个酒店在不同网站下的评论总数
	 * 
	 * @param doc
	 * @return
	 */
	protected abstract int resolveReviewCount(Document doc);

	/**
	 * t_review_score double nullable 评论的分数 酒店在不同网站下的评论分数
	 */
	protected abstract double resolveReviewScore(Document doc);

	/**
	 * t_review_full_score double nullable 评分的满分 每个网站的评分满分不同，有十分制，五分制等
	 * 
	 * @param doc
	 * @return
	 */
	protected abstract double resolveReviewFullScore(Document doc);

	/**
	 * t_review_level Varchar(8) nullable 评分的级别 酒店在每个网站上的评分统计出的一个级别(优秀，良好等)
	 * 
	 * @param doc
	 * @return
	 */
	protected abstract String resolveReviewLevel(Document doc);

	/**
	 * t_review_rank int nullable 酒店排名，暂时只有道道有这个排名信息
	 * 
	 * @param doc
	 * @return
	 */
	protected abstract int resolveReviewRank(Document doc);

	/**
	 * 解析评论的记录数，只需要五条记录
	 */
	protected abstract List<HotelReviewRecordBean> resolveReviewRecords(
			Document doc);

	private void logDebug(String desc) {
		logger.debug("hotelId=" + task.getHotelId() + "|reviewId="
				+ task.getReviewId() + "|reviewType=" + task.getReviewType()
				+ "|" + desc);
	}
}
